<?php 
session_start();
ob_start();
?>
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // Always modified
header("Cache-Control: private, no-store, no-cache, must-revalidate"); // HTTP/1.1 
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
?>
<?php include ("ewconfig.php") ?>
<?php include ("db.php") ?>
<?php include ("pageinfo.php") ?>
<?php include ("advsecu.php") ?>
<?php include ("phpmkrfn.php") ?>
<?php include ("ewupload.php") ?>
<?php

// Initialize common variables
$x_id = NULL;
$ox_id = NULL;
$z_id = NULL;
$ar_x_id = NULL;
$ari_x_id = NULL;
$x_idList = NULL;
$x_idChk = NULL;
$cbo_x_id_js = NULL;
$x_status = NULL;
$ox_status = NULL;
$z_status = NULL;
$ar_x_status = NULL;
$ari_x_status = NULL;
$x_statusList = NULL;
$x_statusChk = NULL;
$cbo_x_status_js = NULL;
$x_image = NULL;
$ox_image = NULL;
$z_image = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$ar_x_image = NULL;
$ari_x_image = NULL;
$x_imageList = NULL;
$x_imageChk = NULL;
$cbo_x_image_js = NULL;
$fs_x_image = 0;
$fn_x_image = "";
$ct_x_image = "";
$wd_x_image = 0;
$ht_x_image = 0;
$a_x_image = "";
$x_title = NULL;
$ox_title = NULL;
$z_title = NULL;
$ar_x_title = NULL;
$ari_x_title = NULL;
$x_titleList = NULL;
$x_titleChk = NULL;
$cbo_x_title_js = NULL;
$x_description = NULL;
$ox_description = NULL;
$z_description = NULL;
$ar_x_description = NULL;
$ari_x_description = NULL;
$x_descriptionList = NULL;
$x_descriptionChk = NULL;
$cbo_x_description_js = NULL;
$x_content = NULL;
$ox_content = NULL;
$z_content = NULL;
$ar_x_content = NULL;
$ari_x_content = NULL;
$x_contentList = NULL;
$x_contentChk = NULL;
$cbo_x_content_js = NULL;
$x_sort = NULL;
$ox_sort = NULL;
$z_sort = NULL;
$ar_x_sort = NULL;
$ari_x_sort = NULL;
$x_sortList = NULL;
$x_sortChk = NULL;
$cbo_x_sort_js = NULL;
$x_author_id = NULL;
$ox_author_id = NULL;
$z_author_id = NULL;
$ar_x_author_id = NULL;
$ari_x_author_id = NULL;
$x_author_idList = NULL;
$x_author_idChk = NULL;
$cbo_x_author_id_js = NULL;
$x_modified_id = NULL;
$ox_modified_id = NULL;
$z_modified_id = NULL;
$ar_x_modified_id = NULL;
$ari_x_modified_id = NULL;
$x_modified_idList = NULL;
$x_modified_idChk = NULL;
$cbo_x_modified_id_js = NULL;
$x_created_date = NULL;
$ox_created_date = NULL;
$z_created_date = NULL;
$ar_x_created_date = NULL;
$ari_x_created_date = NULL;
$x_created_dateList = NULL;
$x_created_dateChk = NULL;
$cbo_x_created_date_js = NULL;
$x_updated_date = NULL;
$ox_updated_date = NULL;
$z_updated_date = NULL;
$ar_x_updated_date = NULL;
$ari_x_updated_date = NULL;
$x_updated_dateList = NULL;
$x_updated_dateChk = NULL;
$cbo_x_updated_date_js = NULL;
?>
<?php

// Load key from QueryString
$x_id = @$_GET["id"];

// Get action
$sAction = @$_POST["a_edit"];
if ($sAction == "") {
	$sAction = "I";	// Display record	
} else {

	// Get fields from form
	$x_id = @$_POST["x_id"];
	$x_status = @$_POST["x_status"];
	$x_image = @$_POST["x_image"];
	$x_title = @$_POST["x_title"];
	$x_description = @$_POST["x_description"];
	$x_content = @$_POST["x_content"];
	$x_sort = @$_POST["x_sort"];
	$x_author_id = @$_POST["x_author_id"];
	$x_modified_id = @$_POST["x_modified_id"];
	$x_created_date = @$_POST["x_created_date"];
	$x_updated_date = @$_POST["x_updated_date"];
}
if (($x_id == "") || (is_null($x_id))) {
	ob_end_clean();
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			header("Location: pageview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
		break;
		default:
			header("Location: pagelist.php");
		break;
	}		
	exit();
}
$conn = phpmkr_db_connect(HOST, USER, PASS, DB, PORT);
switch ($sAction) {
	case "I": // Display record
		if (!LoadData($conn)) { // Load record
			$_SESSION[ewSessionMessage] = "No records found";
			phpmkr_db_close($conn);
			ob_end_clean();
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			header("Location: pageview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
		break;
		default:
			header("Location: pagelist.php");
		break;
	}	
			exit();
		}
		break;
	case "U": // Update
		if (EditData($conn)) { // Update record
			$_SESSION[ewSessionMessage] = "Update Record Successful";
			phpmkr_db_close($conn);
			ob_end_clean();
			switch($_SESSION["session_cms_mode"]){
				case "edit":
					header("Location: pageview.php?id=". $_SESSION["session_cms_id"] . "&mode=" . $_SESSION["session_cms_mode"]);
				break;
				default:
					header("Location: pagelist.php");
				break;
			}	
			exit();
		}
		break;
}
?>
<?php include ("header.php") ?>
<script type="text/javascript">
<!--
EW_LookupFn = "ewlookup.php"; // ewlookup file name
EW_AddOptFn = "ewaddopt.php"; // ewaddopt.php file name

//-->
</script>
<script type="text/javascript" src="ewp.js"></script>
<script type="text/javascript">
<!--
EW_dateSep = "-"; // set date separator
EW_UploadAllowedFileExt = "gif,jpg,jpeg,bmp,png"; // allowed upload file extension

//-->
</script>
<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_status && !EW_hasValue(EW_this.x_status, "RADIO")) {
	if (!EW_onError(EW_this, EW_this.x_status, "RADIO", "Please enter required field - Status"))
		return false;
}
if (EW_this.x_image && !EW_checkfiletype(EW_this.x_image.value)) { 
	if (!EW_onError(EW_this, EW_this.x_image, "FILE", "File type is not allowed.")) 
	return false; 
}
if (EW_this.x_title && !EW_hasValue(EW_this.x_title, "TEXT")) {
	if (!EW_onError(EW_this, EW_this.x_title, "TEXT", "Please enter required field - Judul"))
		return false;
}
return true;
}

//-->
</script>
<script type="text/javascript">
<!--
	var EW_DHTMLEditors = [];

//-->
</script>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td class="admin_td_title">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
		  <tr>
			<td class="admin_title"><?=str_replace(","," &gt; ",$_SESSION['session_cms_title'])?> &raquo; Edit &nbsp;</td>
		   </tr>
		 </table>
 	</td>
  </tr>
</table>
<?php
	switch($_SESSION["session_cms_mode"]){
		case "edit":
			echo "<br><a href='pageview.php' class='button-modify'>View</a><br />";
		break;
		default:
			echo "<br><a href='pagelist.php'>Back to List</a><br />";
		break;
	}
?>
<form name="fpageedit" id="fpageedit" action="pageedit.php" method="post" enctype="multipart/form-data" onSubmit="return EW_checkMyForm(this);">
<p>
	<input type="hidden" name="a_edit" value="U">
	<input type="hidden" name="EW_Max_File_Size" value="2000000">
<?php
if (@$_SESSION[ewSessionMessage] <> "") {
?>
<p><span class="ewmsg"><?php echo $_SESSION[ewSessionMessage]; ?></span></p>
<?php
	$_SESSION[ewSessionMessage] = ""; // Clear message
}
?>
	<table class="ewTable">
<input type="hidden" id="x_id" name="x_id" value="<?php echo @$x_id; ?>">
		<tr style="display:none;">
			<td class="ewTableHeader"><span>Status<span class='ewmsg'>&nbsp;*</span></span></td>
			<td class="ewTableAltRow"><span id="cb_x_status">
<?php echo RenderControl(1, 0, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "A") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("A"); ?>">
<?php echo get_icon_status("A"); ?>
<?php echo RenderControl(1, 0, 5, 2); ?>
<?php echo RenderControl(1, 1, 5, 1); ?>
<input type="radio" name="x_status"<?php if (@$x_status == "I") { ?> checked<?php } ?> value="<?php echo htmlspecialchars("I"); ?>">
<?php echo get_icon_status("I"); ?>
<?php echo RenderControl(1, 1, 5, 2); ?>
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>Gambar</span></td>
			<td class="ewTableAltRow"><span id="cb_x_image">
<?php if ((!is_null($x_image)) && $x_image <> "") {  ?>
<img src="<?php echo ewUploadPathEx(False, "../uploads/page/thumbs/") . $x_image ?>" border=0 width="100" align="left" style="padding-right:10px">

<input type="radio" name="a_x_image" value="1" checked style="display:none"><!--Keep&nbsp;-->
<input type="radio" name="a_x_image" value="2" style="float:left;"><span style="font-size:11px">Hapus Gambar&nbsp;</span>
<input type="radio" name="a_x_image" value="3" style="display:none"><!--Replace--><br>
<?php } else {?>
<input type="hidden" name="a_x_image" value="3">
<?php } ?>
<input type="file" id="x_image" name="x_image" size="60" onChange="if (this.form.a_x_image[2]) this.form.a_x_image[2].checked=true;">
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>Judul<span class='ewmsg'>&nbsp;*</span></span></td>
			<td class="ewTableAltRow"><span id="cb_x_title">
<input type="text" name="x_title" id="x_title" size="60" maxlength="255" value="<?php echo htmlspecialchars(@$x_title) ?>">
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader" style="padding-top:10px;"><span>Deskripsi</span></td>
			<td class="ewTableAltRow" style="padding-top:10px;padding-bottom:10px;"><span id="cb_x_description">
<textarea cols="100" rows="4" id="x_description" name="x_description" style="padding:5px;"><?php echo @$x_description; ?></textarea>
</span></td>
		</tr>
		<tr>
			<td class="ewTableHeader"><span>Isi</span></td>
			<td class="ewTableAltRow"><span id="cb_x_content">
<textarea cols="35" rows="4" id="x_content" name="x_content"><?php echo @$x_content; ?></textarea>
</span></td>
		</tr>
<input type="hidden" id="x_modified_id" name="x_modified_id" value="<?php echo $SessionAuthorId; ?>">
<input type="hidden" id="x_updated_date" name="x_updated_date" value="<?php echo date('Y-m-d h:i:s'); ?>">
	</table>
	<p>
	<input type="submit" name="btnAction" id="btnAction" value="SIMPAN">
	<input type="button" name="btnCancel" id="btnCancel" value="BATAL" onclick="javascript:history.back(-1)" />
</form>
<?php include ("footer.php") ?>
<?php
phpmkr_db_close($conn);
?>
<?php

//-------------------------------------------------------------------------------
// Function LoadData
// - Variables setup: field variables

function LoadData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;
	if (!is_numeric($x_id)) return false;
	$x_id =  (get_magic_quotes_gpc()) ? stripslashes($x_id) : $x_id;
	$sFilter = str_replace("@id", AdjustSql($x_id), $sFilter); // Replace key value
	$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
	$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
	if (phpmkr_num_rows($rs) == 0) {
		$bLoadData = false;
	} else {
		$bLoadData = true;
		$row = phpmkr_fetch_array($rs);

		// Get the field contents
		$GLOBALS["x_id"] = $row["id"];
		$GLOBALS["x_status"] = $row["status"];
		$GLOBALS["x_image"] = $row["image"];
		$GLOBALS["x_title"] = $row["title"];
		$GLOBALS["x_description"] = $row["description"];
		$GLOBALS["x_content"] = $row["content"];
		$GLOBALS["x_sort"] = $row["sort"];
		$GLOBALS["x_author_id"] = $row["author_id"];
		$GLOBALS["x_modified_id"] = $row["modified_id"];
		$GLOBALS["x_created_date"] = $row["created_date"];
		$GLOBALS["x_updated_date"] = $row["updated_date"];
	}
	phpmkr_free_result($rs);
	return $bLoadData;
}
?>
<?php

//-------------------------------------------------------------------------------
// Function EditData
// - Variables used: field variables

function EditData($conn)
{
	global $x_id;
	$sFilter = ewSqlKeyWhere;
	if (!is_numeric($x_id)) return false;
	$sTmp =  (get_magic_quotes_gpc()) ? stripslashes($x_id) : $x_id;
	$sFilter = str_replace("@id", AdjustSql($sTmp), $sFilter); // Replace key value
	$sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
	$rs = phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);

	// Get old recordset
	$oldrs = phpmkr_fetch_array($rs);
	if (phpmkr_num_rows($rs) == 0) {
		return false; // Update Failed
	} else {

		// Check file size
		$EW_MaxFileSize = @$_POST["EW_Max_File_Size"];
		$x_id = @$_POST["x_id"];
		$x_status = @$_POST["x_status"];

		// Check the file size
		if (!empty($_FILES["x_image"]["size"])) {
			if (!empty($EW_MaxFileSize) && $_FILES["x_image"]["size"] > $EW_MaxFileSize) {
				die(str_replace("%s", $EW_MaxFileSize, "Max. file size (%s bytes) exceeded."));
			}
		}
		$fn_x_image = @$_FILES["x_image"]["name"];

		// Check the file type
		if (!empty($fn_x_image)) {
			if (!ewUploadAllowedFileExt($fn_x_image)) {
				die("File type is not allowed.");
			}
		}
		$ct_x_image = @$_POST["x_image"];
		$x_image = @$_POST[ "x_image"];
		$wd_x_image = @$_POST["wd_x_image"];
		$ht_x_image = @$_POST["ht_x_image"];
		$a_x_image =  @$_POST["a_x_image"];
		$x_title = @$_POST["x_title"];
		$x_description = @$_POST["x_description"];
		$x_content = @$_POST["x_content"];
		$x_modified_id = @$_POST["x_modified_id"];
		$x_updated_date = @$_POST["x_updated_date"];
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_status"]) : $GLOBALS["x_status"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`status`"] = $theValue;
		if ($a_x_image == "2") { // Remove
			$fieldList["`image`"] = "NULL";
			$ox_image = $oldrs["image"];
			$sTmpFolder = ewUploadPathEx(True, "../uploads/page/");
			if ($ox_image <> "") { 
				@unlink($sTmpFolder . $ox_image); 
				@unlink($sTmpFolder ."thumbs/". $ox_image);		
			}
		} else if ($a_x_image == "3") { // Update
			if (is_uploaded_file($_FILES["x_image"]["tmp_name"])) {
				$sTmpFolder = ewUploadPathEx(true, "../uploads/page/");
				$ox_image = $oldrs["image"];	
				if ($ox_image <> ""){
					@unlink($sTmpFolder . $ox_image);
					@unlink($sTmpFolder ."thumbs/". $ox_image);		
				}
				$theName = ewUploadFileNameEx($sTmpFolder, $_FILES["x_image"]["name"]);
				$destfile = $sTmpFolder . $theName;
				
				/* OLD MOVE
				if (!move_uploaded_file($_FILES["x_image"]["tmp_name"], $destfile)) // Move file to destination path
					die("" . $destfile);
				@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);
				*/
				
				list($width, $height, $type, $attr) = getimagesize($_FILES["x_image"]["tmp_name"]);
				
				if($width > 500){
				// Move & Resize Uploaded File
					$resize = new SimpleImage(); 
					$resize->load($_FILES["x_image"]["tmp_name"]); 
					$resize->resizeToWidth(500); 				
					$resize->save($destfile);
					@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);
				}else{
					if (!move_uploaded_file($_FILES["x_image"]["tmp_name"], $destfile)) // Move file to destination path
						die("" . $destfile);	
					@chmod($destfile, defined(EW_UploadedFileMode) ? EW_UploadedFileMode : 0666);					
				}
				
				//Resize Image
				$resize2 = new SimpleImage();
				$resizeName=$theName;
				$resizePath="../../uploads/page/thumbs/";
				$theResize=$resizePath.$resizeName;
				$resize2->load($destfile); 
				$resize2->resizeToWidth(300); 				
				$resize2->save($theResize);
				
				// File name
				$theName = (!get_magic_quotes_gpc()) ? addslashes($theName) : $theName;
				$fieldList["`image`"] = " '" . $theName . "'";
				@unlink($_FILES["x_image"]["tmp_name"]);
			}
		}
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_title"]) : $GLOBALS["x_title"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`title`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_description"]) : $GLOBALS["x_description"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`description`"] = $theValue;
		$theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_content"]) : $GLOBALS["x_content"]; 
		$theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
		$fieldList["`content`"] = $theValue;
		$theValue = ($GLOBALS["x_modified_id"] != "") ? intval($GLOBALS["x_modified_id"]) : "NULL";
		$fieldList["`modified_id`"] = $theValue;
		$theValue = ($GLOBALS["x_updated_date"] != "") ? " '" . ConvertDateToMysqlFormat($GLOBALS["x_updated_date"]) . "'" : "Null";
		$fieldList["`updated_date`"] = $theValue;

		// Updating event
		if (Recordset_Updating($fieldList, $oldrs)) {

			// Update
			$sSql = "UPDATE `page` SET ";
			foreach ($fieldList as $key=>$temp) {
				$sSql .= "$key = $temp, ";
			}
			if (substr($sSql, -2) == ", ") {
				$sSql = substr($sSql, 0, strlen($sSql)-2);
			}
			$sSql .= " WHERE " . $sFilter;
			phpmkr_query($sSql,$conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
			$result = (phpmkr_affected_rows($conn) >= 0);

			// Updated event
			if ($result) Recordset_Updated($fieldList, $oldrs);
		} else {
			$result = false; // Update Failed
		}
	}
	return $result;
}

// Updating Event
function Recordset_Updating(&$newrs, $oldrs)
{

	// Enter your customized codes here
	return true;
}

// Updated event
function Recordset_Updated($newrs, $oldrs)
{
	$table = "page";
}
?>
